package com.ruoyi.web.controller.web3j;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.web3jutil.Qingongjianxue;
import com.ruoyi.web3jutil.Web3jUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.web3j.crypto.Credentials;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.tuples.generated.Tuple2;
import org.web3j.tuples.generated.Tuple5;
import org.web3j.tuples.generated.Tuple6;
import org.web3j.tx.gas.DefaultGasProvider;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 勤工俭学系统控制器
 * 
 * @author ruoyi
 */
@Api(tags = "勤工俭学系统")
@RestController
@RequestMapping("/web3j/qingongjianxue")
public class QingongjianxueController {

    private static final String CONTRACT_ADDRESS = "0x358AA13c52544ECCEF6B0ADD0f801012ADAD5eE3"; // 合约地址，需要替换为实际部署的地址

    /**
     * 加载合约
     * 
     * @param privateKey 私钥
     * @return 合约实例
     * @throws Exception 加载失败时抛出异常
     */
    private Qingongjianxue loadContract(String privateKey) throws Exception {
        Web3j web3j = Web3jUtil.getWeb3j();
        Credentials credentials = Credentials.create(privateKey);
        return Qingongjianxue.load(CONTRACT_ADDRESS, web3j, credentials, new DefaultGasProvider());
    }

    /**
     * 学生注册
     * 
     * @param privateKey 私钥
     * @param name 姓名
     * @param studentId 学号
     * @return 注册结果
     */
    @ApiOperation(value = "学生注册", notes = "学生用户注册到勤工俭学系统")
    @PostMapping("/student/register")
    public AjaxResult studentRegister(@RequestParam String privateKey, 
                                     @RequestParam String name, 
                                     @RequestParam BigInteger studentId) {
        try {
            Qingongjianxue contract = loadContract(privateKey);
            TransactionReceipt receipt = contract.registerStudent(name, studentId).send();
            return AjaxResult.success("学生注册成功", receipt.getTransactionHash());
        } catch (Exception e) {
            return AjaxResult.error("学生注册失败: " + e.getMessage());
        }
    }

    /**
     * 企业注册
     * 
     * @param privateKey 私钥
     * @param name 企业名称
     * @return 注册结果
     */
    @ApiOperation(value = "企业注册", notes = "企业用户注册到勤工俭学系统")
    @PostMapping("/company/register")
    public AjaxResult companyRegister(@RequestParam String privateKey, 
                                     @RequestParam String name) {
        try {
            Qingongjianxue contract = loadContract(privateKey);
            
            // 获取当前账户地址
            Credentials credentials = Credentials.create(privateKey);
            String address = credentials.getAddress();
            
            // 先检查该地址是否已经注册为企业
            try {
                Tuple5<String, String, BigInteger, BigInteger, Boolean> employerInfo = contract.employers(address).send();
                if (employerInfo != null && employerInfo.component5()) {
                    // 如果isRegistered为true，表示已经注册
                    return AjaxResult.error("该地址已经注册为企业");
                }
            } catch (Exception e) {
                // 如果查询失败，继续尝试注册
            }
            
            TransactionReceipt receipt = contract.registerEmployer(name).send();
            return AjaxResult.success("企业注册成功", receipt.getTransactionHash());
        } catch (Exception e) {
            // 捕获并处理特定的错误信息
            String errorMsg = e.getMessage();
            if (errorMsg.contains("Employer already registered")) {
                return AjaxResult.error("该地址已经注册为企业");
            }
            return AjaxResult.error("企业注册失败: " + errorMsg);
        }
    }

    /**
     * 设置企业信誉等级
     * 
     * @param privateKey 管理员私钥
     * @param companyAddress 企业地址
     * @param creditLevel 信誉等级
     * @return 设置结果
     */
    @ApiOperation(value = "设置企业信誉等级", notes = "管理员设置企业的信誉等级")
    @PostMapping("/company/credit")
    public AjaxResult setCompanyCredit(@RequestParam String privateKey, 
                                      @RequestParam String companyAddress, 
                                      @RequestParam BigInteger creditLevel) {
        try {
            Qingongjianxue contract = loadContract(privateKey);
            TransactionReceipt receipt = contract.adjustReputation(companyAddress, creditLevel).send();
            return AjaxResult.success("设置企业信誉等级成功", receipt.getTransactionHash());
        } catch (Exception e) {
            return AjaxResult.error("设置企业信誉等级失败: " + e.getMessage());
        }
    }

    /**
     * 设置管理员
     * 
     * @param privateKey 合约拥有者私钥
     * @param adminAddress 管理员地址
     * @param role 角色
     * @return 设置结果
     */
    @ApiOperation(value = "设置管理员", notes = "合约拥有者设置管理员角色")
    @PostMapping("/admin/set")
    public AjaxResult setAdmin(@RequestParam String privateKey, 
                              @RequestParam String adminAddress, 
                              @RequestParam BigInteger role) {
        try {
            Qingongjianxue contract = loadContract(privateKey);
            TransactionReceipt receipt = contract.registerAdmin(adminAddress, role).send();
            return AjaxResult.success("设置管理员成功", receipt.getTransactionHash());
        } catch (Exception e) {
            return AjaxResult.error("设置管理员失败: " + e.getMessage());
        }
    }

    /**
     * 发布工作
     * 
     * @param privateKey 企业私钥
     * @param title 工作标题
     * @param salary 薪资
     * @return 发布结果
     */
    @ApiOperation(value = "发布工作", notes = "企业发布勤工俭学工作")
    @PostMapping("/job/publish")
    public AjaxResult publishJob(@RequestParam String privateKey, 
                                @RequestParam String title, 
                                @RequestParam BigInteger salary) {
        try {
            Qingongjianxue contract = loadContract(privateKey);
            // 默认工作时长为0
            TransactionReceipt receipt = contract.postJob(title, salary, BigInteger.ZERO).send();
            return AjaxResult.success("发布工作成功", receipt.getTransactionHash());
        } catch (Exception e) {
            return AjaxResult.error("发布工作失败: " + e.getMessage());
        }
    }

    /**
     * 申请工作
     * 
     * @param privateKey 学生私钥
     * @param jobId 工作ID
     * @return 申请结果
     */
    @ApiOperation(value = "申请工作", notes = "学生申请勤工俭学工作")
    @PostMapping("/job/apply")
    public AjaxResult applyForJob(@RequestParam String privateKey, 
                                 @RequestParam BigInteger jobId) {
        try {
            Qingongjianxue contract = loadContract(privateKey);
            TransactionReceipt receipt = contract.applyJob(jobId).send();
            return AjaxResult.success("申请工作成功", receipt.getTransactionHash());
        } catch (Exception e) {
            return AjaxResult.error("申请工作失败: " + e.getMessage());
        }
    }

    /**
     * 选择工人
     * 
     * @param privateKey 企业私钥
     * @param jobId 工作ID
     * @param workers 工人地址列表
     * @return 选择结果
     */
    @ApiOperation(value = "选择工人", notes = "企业选择工人完成工作")
    @PostMapping("/job/select-workers")
    public AjaxResult selectWorkers(@RequestParam String privateKey, 
                                   @RequestParam BigInteger jobId, 
                                   @RequestParam List<String> workers) {
        try {
            Qingongjianxue contract = loadContract(privateKey);
            TransactionReceipt receipt = contract.assignJob(jobId, workers).send();
            return AjaxResult.success("选择工人成功", receipt.getTransactionHash());
        } catch (Exception e) {
            return AjaxResult.error("选择工人失败: " + e.getMessage());
        }
    }

    /**
     * 完成工作
     * 
     * @param privateKey 企业私钥
     * @param jobId 工作ID
     * @return 完成结果
     */
    @ApiOperation(value = "完成工作", notes = "企业确认工作完成并支付薪资")
    @PostMapping("/job/complete")
    public AjaxResult completeJob(@RequestParam String privateKey, 
                                 @RequestParam BigInteger jobId) {
        try {
            Qingongjianxue contract = loadContract(privateKey);
            TransactionReceipt receipt = contract.completeJob(jobId).sendAsync().get();
            return AjaxResult.success("完成工作成功", receipt.getTransactionHash());
        } catch (Exception e) {
            return AjaxResult.error("完成工作失败: " + e.getMessage());
        }
    }

    /**
     * 获取所有企业
     * 
     * @return 企业列表
     */
    @ApiOperation(value = "获取所有企业", notes = "获取系统中所有注册的企业")
    @GetMapping("/companies")
    public AjaxResult getAllCompanies() {
        try {
            // 使用随机主账户加载合约，只读操作不会消耗gas
            String[] mainAccount = Web3jUtil.getMainAddress();
            Qingongjianxue contract = loadContract(mainAccount[1]);
            
            // 手动构建企业列表
            List<Tuple5<String, String, BigInteger, BigInteger, Boolean>> companies = new ArrayList<>();
            
            // 尝试获取企业，直到遇到异常（表示已经超出列表范围）
            BigInteger index = BigInteger.ZERO;
            boolean continueLoop = true;
            
            while (continueLoop) {
                try {
                    Tuple5<String, String, BigInteger, BigInteger, Boolean> employer = contract.employerList(index).send();
                    // 检查返回的地址是否为空地址，如果是则跳过
                    if (employer != null && !employer.component1().equals("0x0000000000000000000000000000000000000000")) {
                        companies.add(employer);
                    }
                    index = index.add(BigInteger.ONE);
                } catch (Exception e) {
                    // 遇到异常，表示已经到达列表末尾或者出现其他错误
                    continueLoop = false;
                }
                
                // 设置一个安全上限，防止无限循环
                if (index.compareTo(BigInteger.valueOf(1000)) > 0) {
                    continueLoop = false;
                }
            }
            
            return AjaxResult.success("获取企业列表成功", companies);
        } catch (Exception e) {
            return AjaxResult.error("获取企业列表失败: " + e.getMessage());
        }
    }

    /**
     * 获取所有工作
     * 
     * @return 工作列表
     */
    @ApiOperation(value = "获取所有工作", notes = "获取系统中所有发布的工作")
    @GetMapping("/jobs")
    public AjaxResult getAllJobs() {
        try {
            // 使用随机主账户加载合约，只读操作不会消耗gas
            String[] mainAccount = Web3jUtil.getMainAddress();
            Qingongjianxue contract = loadContract(mainAccount[1]);
            
            // 手动构建工作列表
            List<Map<String, Object>> jobs = new ArrayList<>();
            
            // 设置一个合理的上限，例如100个工作
            int maxJobs = 100;
            
            // 遍历所有可能的工作ID
            for (int i = 1; i <= maxJobs; i++) {
                try {
                    BigInteger jobId = BigInteger.valueOf(i);
                    Tuple6<BigInteger, String, String, BigInteger, BigInteger, BigInteger> job = contract.jobs(jobId).send();
                    
                    // 检查工作是否有效（例如，检查标题是否为空，雇主地址是否为空地址）
                    if (job != null && job.component2() != null && !job.component2().isEmpty() 
                            && !job.component3().equals("0x0000000000000000000000000000000000000000")) {
                        
                        // 创建一个Map来存储工作信息
                        Map<String, Object> jobInfo = new HashMap<>();
                        jobInfo.put("id", job.component1());
                        jobInfo.put("title", job.component2());
                        jobInfo.put("employer", job.component3());
                        jobInfo.put("salary", job.component4());
                        jobInfo.put("status", job.component5());
                        jobInfo.put("workHours", job.component6());
                        
                        jobs.add(jobInfo);
                    }
                } catch (Exception e) {
                    // 如果获取某个工作信息失败，继续获取下一个
                    continue;
                }
            }
            
            return AjaxResult.success("获取工作列表成功", jobs);
        } catch (Exception e) {
            return AjaxResult.error("获取工作列表失败: " + e.getMessage());
        }
    }

    /**
     * 获取管理员信息
     * 
     * @param adminAddress 管理员地址
     * @return 管理员信息
     */
    @ApiOperation(value = "获取管理员信息", notes = "获取指定地址的管理员信息")
    @GetMapping("/admin/{address}")
    public AjaxResult getAdminInfo(@PathVariable("address") String adminAddress) {
        try {
            // 使用随机主账户加载合约，只读操作不会消耗gas
            String[] mainAccount = Web3jUtil.getMainAddress();
            Qingongjianxue contract = loadContract(mainAccount[1]);
            
            Tuple2<String, BigInteger> adminInfo = contract.admins(adminAddress).send();
            return AjaxResult.success("获取管理员信息成功", adminInfo);
        } catch (Exception e) {
            return AjaxResult.error("获取管理员信息失败: " + e.getMessage());
        }
    }

    /**
     * 获取企业信息
     * 
     * @param companyAddress 企业地址
     * @return 企业信息
     */
    @ApiOperation(value = "获取企业信息", notes = "获取指定地址的企业信息")
    @GetMapping("/company/{address}")
    public AjaxResult getCompanyInfo(@PathVariable("address") String companyAddress) {
        try {
            // 使用随机主账户加载合约，只读操作不会消耗gas
            String[] mainAccount = Web3jUtil.getMainAddress();
            Qingongjianxue contract = loadContract(mainAccount[1]);
            
            // 从getAllCompanies方法中复用代码，获取所有企业
            List<Tuple5<String, String, BigInteger, BigInteger, Boolean>> companies = new ArrayList<>();
            BigInteger index = BigInteger.ZERO;
            boolean continueLoop = true;
            
            while (continueLoop) {
                try {
                    Tuple5<String, String, BigInteger, BigInteger, Boolean> employer = contract.employerList(index).send();
                    if (employer != null && !employer.component1().equals("0x0000000000000000000000000000000000000000")) {
                        companies.add(employer);
                    }
                    index = index.add(BigInteger.ONE);
                } catch (Exception e) {
                    continueLoop = false;
                }
                
                if (index.compareTo(BigInteger.valueOf(100)) > 0) {
                    continueLoop = false;
                }
            }
            
            // 在获取到的企业列表中查找匹配的企业地址
            for (Tuple5<String, String, BigInteger, BigInteger, Boolean> company : companies) {
                if (company.component1().equalsIgnoreCase(companyAddress)) {
                    return AjaxResult.success("获取企业信息成功", company);
                }
            }
            
            return AjaxResult.error("未找到该企业信息");
        } catch (Exception e) {
            return AjaxResult.error("获取企业信息失败: " + e.getMessage());
        }
    }

    /**
     * 获取学生信息
     * 
     * @param studentAddress 学生地址
     * @return 学生信息
     */
    @ApiOperation(value = "获取学生信息", notes = "获取指定地址的学生信息")
    @GetMapping("/student/{address}")
    public AjaxResult getStudentInfo(@PathVariable("address") String studentAddress) {
        try {
            // 使用随机主账户加载合约，只读操作不会消耗gas
            String[] mainAccount = Web3jUtil.getMainAddress();
            Qingongjianxue contract = loadContract(mainAccount[1]);
            
            Tuple5<String, String, BigInteger, Boolean, BigInteger> studentInfo = contract.students(studentAddress).send();
            return AjaxResult.success("获取学生信息成功", studentInfo);
        } catch (Exception e) {
            return AjaxResult.error("获取学生信息失败: " + e.getMessage());
        }
    }

    /**
     * 获取工作信息
     * 
     * @param jobId 工作ID
     * @return 工作信息
     */
    @ApiOperation(value = "获取工作信息", notes = "获取指定ID的工作信息")
    @GetMapping("/job/{id}")
    public AjaxResult getJobInfo(@PathVariable("id") BigInteger jobId) {
        try {
            // 使用随机主账户加载合约，只读操作不会消耗gas
            String[] mainAccount = Web3jUtil.getMainAddress();
            Qingongjianxue contract = loadContract(mainAccount[1]);
            
            Tuple6<BigInteger, String, String, BigInteger, BigInteger, BigInteger> jobInfo = contract.jobs(jobId).send();
            return AjaxResult.success("获取工作信息成功", jobInfo);
        } catch (Exception e) {
            return AjaxResult.error("获取工作信息失败: " + e.getMessage());
        }
    }

    /**
     * 支付薪资
     * 
     * @param privateKey 企业私钥
     * @param jobId 工作ID
     * @return 支付结果
     */
    @ApiOperation(value = "支付薪资", notes = "企业支付工作薪资")
    @PostMapping("/job/pay")
    public AjaxResult paySalary(@RequestParam String privateKey, 
                               @RequestParam BigInteger jobId) {
        try {
            Qingongjianxue contract = loadContract(privateKey);
            // 添加weiValue参数，设置为0表示不发送以太币
            TransactionReceipt receipt = contract.paySalary(jobId, BigInteger.ZERO).send();
            return AjaxResult.success("支付薪资成功", receipt.getTransactionHash());
        } catch (Exception e) {
            return AjaxResult.error("支付薪资失败: " + e.getMessage());
        }
    }

    /**
     * 获取管理员列表信息
     * 
     * @param index 索引
     * @return 管理员信息
     */
    @ApiOperation(value = "获取管理员列表信息", notes = "根据索引获取管理员列表中的管理员信息")
    @GetMapping("/admin-list/{index}")
    public AjaxResult getAdminListInfo(@PathVariable("index") BigInteger index) {
        try {
            // 使用随机主账户加载合约，只读操作不会消耗gas
            String[] mainAccount = Web3jUtil.getMainAddress();
            Qingongjianxue contract = loadContract(mainAccount[1]);
            
            Tuple2<String, BigInteger> adminInfo = contract.adminList(index).send();
            return AjaxResult.success("获取管理员列表信息成功", adminInfo);
        } catch (Exception e) {
            return AjaxResult.error("获取管理员列表信息失败: " + e.getMessage());
        }
    }

    /**
     * 获取企业列表信息
     * 
     * @param index 索引
     * @return 企业信息
     */
    @ApiOperation(value = "获取企业列表信息", notes = "根据索引获取企业列表中的企业信息")
    @GetMapping("/employer-list/{index}")
    public AjaxResult getEmployerListInfo(@PathVariable("index") BigInteger index) {
        try {
            // 使用随机主账户加载合约，只读操作不会消耗gas
            String[] mainAccount = Web3jUtil.getMainAddress();
            Qingongjianxue contract = loadContract(mainAccount[1]);
            
            Tuple5<String, String, BigInteger, BigInteger, Boolean> employerInfo = contract.employerList(index).send();
            return AjaxResult.success("获取企业列表信息成功", employerInfo);
        } catch (Exception e) {
            return AjaxResult.error("获取企业列表信息失败: " + e.getMessage());
        }
    }

    /**
     * 获取所有管理员
     * 
     * @return 管理员列表
     */
    @ApiOperation(value = "获取所有管理员", notes = "获取系统中所有注册的管理员")
    @GetMapping("/admins")
    public AjaxResult getAllAdmins() {
        try {
            // 使用随机主账户加载合约，只读操作不会消耗gas
            String[] mainAccount = Web3jUtil.getMainAddress();
            Qingongjianxue contract = loadContract(mainAccount[1]);
            
            List<Tuple2<String, BigInteger>> admins = contract.getAllAdmins().send();
            return AjaxResult.success("获取管理员列表成功", admins);
        } catch (Exception e) {
            return AjaxResult.error("获取管理员列表失败: " + e.getMessage());
        }
    }

    /**
     * 获取所有学生
     * 
     * @return 学生列表
     */
    @ApiOperation(value = "获取所有学生", notes = "获取系统中所有注册的学生")
    @GetMapping("/students")
    public AjaxResult getAllStudents() {
        try {
            // 使用随机主账户加载合约，只读操作不会消耗gas
            String[] mainAccount = Web3jUtil.getMainAddress();
            Qingongjianxue contract = loadContract(mainAccount[1]);
            
            List<Tuple5<String, String, BigInteger, Boolean, BigInteger>> students = contract.getAllStudents().send();
            return AjaxResult.success("获取学生列表成功", students);
        } catch (Exception e) {
            return AjaxResult.error("获取学生列表失败: " + e.getMessage());
        }
    }

    /**
     * 获取工作计数器
     * 
     * @return 工作计数器值
     */
    @ApiOperation(value = "获取工作计数器", notes = "获取系统中的工作计数器值")
    @GetMapping("/job-counter")
    public AjaxResult getJobCounter() {
        try {
            // 使用随机主账户加载合约，只读操作不会消耗gas
            String[] mainAccount = Web3jUtil.getMainAddress();
            Qingongjianxue contract = loadContract(mainAccount[1]);
            
            BigInteger counter = contract.jobCounter().send();
            return AjaxResult.success("获取工作计数器成功", counter);
        } catch (Exception e) {
            return AjaxResult.error("获取工作计数器失败: " + e.getMessage());
        }
    }

    /**
     * 获取合约拥有者
     * 
     * @return 合约拥有者地址
     */
    @ApiOperation(value = "获取合约拥有者", notes = "获取合约的拥有者地址")
    @GetMapping("/owner")
    public AjaxResult getOwner() {
        try {
            // 使用随机主账户加载合约，只读操作不会消耗gas
            String[] mainAccount = Web3jUtil.getMainAddress();
            Qingongjianxue contract = loadContract(mainAccount[1]);
            
            String ownerAddress = contract.owner().send();
            return AjaxResult.success("获取合约拥有者成功", ownerAddress);
        } catch (Exception e) {
            return AjaxResult.error("获取合约拥有者失败: " + e.getMessage());
        }
    }

    /**
     * 获取学生列表信息
     * 
     * @param studentAddress 学生地址
     * @return 学生信息
     */
    @ApiOperation(value = "获取学生列表信息", notes = "根据地址获取学生列表中的学生信息")
    @GetMapping("/student-list/{address}")
    public AjaxResult getStudentListInfo(@PathVariable("address") String studentAddress) {
        try {
            // 使用随机主账户加载合约，只读操作不会消耗gas
            String[] mainAccount = Web3jUtil.getMainAddress();
            Qingongjianxue contract = loadContract(mainAccount[1]);
            
            // 将String类型的地址转换为BigInteger类型
            BigInteger studentIndex = new BigInteger(studentAddress);
            Tuple5<String, String, BigInteger, Boolean, BigInteger> studentInfo = contract.studentList(studentIndex).send();
            return AjaxResult.success("获取学生列表信息成功", studentInfo);
        } catch (Exception e) {
            return AjaxResult.error("获取学生列表信息失败: " + e.getMessage());
        }
    }

    /**
     * 获取工作申请者列表
     * 
     * @param jobId 工作ID
     * @return 申请者地址列表
     */
    @ApiOperation(value = "获取工作申请者", notes = "获取申请指定工作的学生列表")
    @GetMapping("/job/{id}/applicants")
    public AjaxResult getJobApplicants(@PathVariable("id") BigInteger jobId) {
        try {
            // 使用随机主账户加载合约，只读操作不会消耗gas
            String[] mainAccount = Web3jUtil.getMainAddress();
            Qingongjianxue contract = loadContract(mainAccount[1]);
            
            List<String> applicants = contract.getJobApplicants(jobId).send();
            return AjaxResult.success("获取工作申请者列表成功", applicants);
        } catch (Exception e) {
            return AjaxResult.error("获取工作申请者列表失败: " + e.getMessage());
        }
    }

    /**
     * 获取工作已选择的学生列表
     * 
     * @param jobId 工作ID
     * @return 已选择的学生地址列表
     */
    @ApiOperation(value = "获取工作已选择的学生", notes = "获取已被选择完成指定工作的学生列表")
    @GetMapping("/job/{id}/selected-students")
    public AjaxResult getJobSelectedStudents(@PathVariable("id") BigInteger jobId) {
        try {
            // 使用随机主账户加载合约，只读操作不会消耗gas
            String[] mainAccount = Web3jUtil.getMainAddress();
            Qingongjianxue contract = loadContract(mainAccount[1]);
            
            List<String> selectedStudents = contract.getJobSelectedStudents(jobId).send();
            return AjaxResult.success("获取工作已选择的学生列表成功", selectedStudents);
        } catch (Exception e) {
            return AjaxResult.error("获取工作已选择的学生列表失败: " + e.getMessage());
        }
    }

    /**
     * 获取学生当前的工作列表
     * 
     * @param studentAddress 学生地址
     * @return 工作ID列表
     */
    @ApiOperation(value = "获取学生当前工作", notes = "获取指定学生当前参与的工作列表")
    @GetMapping("/student/{address}/current-jobs")
    public AjaxResult getStudentCurrentJobs(@PathVariable("address") String studentAddress) {
        try {
            // 使用随机主账户加载合约，只读操作不会消耗gas
            String[] mainAccount = Web3jUtil.getMainAddress();
            Qingongjianxue contract = loadContract(mainAccount[1]);
            
            List<BigInteger> currentJobs = contract.getStudentCurrentJobs(studentAddress).send();
            return AjaxResult.success("获取学生当前工作列表成功", currentJobs);
        } catch (Exception e) {
            return AjaxResult.error("获取学生当前工作列表失败: " + e.getMessage());
        }
    }
}